Option Strict On

Imports Excel = Microsoft.Office.Interop.Excel

Public Class TemplateProperty
    Implements ITemplateProperty

    Private _location As Excel.Range
    Private _value As Object
    Private _name As String

    Public Sub New(ByRef location As Excel.Range, ByRef properties As Object, ByVal name As String)
        If (properties Is Nothing) Then
            Throw New ArgumentException("Properties object was not supplyed!")
        End If

        Me._name = name
        Me._location = location

        Dim type As System.Type = properties.GetType()
        Dim pInfo As System.Reflection.PropertyInfo = Type.GetProperty(name)
        If (pInfo Is Nothing) Then
            Throw New ArgumentException( _
            String.Format("There are no property with name: '{0}' in supplyed properties object!", name))
        End If
        Me._value = type.GetProperty(name).GetValue(properties, Nothing)

    End Sub

    Public Sub DeployProperty() Implements ITemplateProperty.DeployProperty
        RangeEvaluator.SetValue(_location, _value)
        Me._IsDeployed = True
    End Sub

    Private _IsDeployed As Boolean = False
    Public ReadOnly Property IsDeployed() As Boolean Implements ITemplateProperty.IsDeployed
        Get
            Return Me._IsDeployed
        End Get
    End Property

    Public Function IsReferenceOnMe(ByVal areaReference As String) As Boolean Implements ITemplateProperty.IsReferenceOnMe
        Try
            Dim o As Object = _location.Worksheet.Range(areaReference & " " & ReferenceAddress).Value
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public ReadOnly Property ReferenceAddress() As String Implements ITemplateProperty.ReferenceAddress
        Get
            Return Me._location.AddressLocal()
        End Get
    End Property
End Class
